package com.zhugang.queue;

/**
 * @program algorithms
 * @description: 基于链表实现的队列
 * @author: chanzhugang
 * @create: 2022/02/22 23:49
 */
public class QueueOnLinkedList {

    private Node head;
    private Node tail;


    /**
     * 入队
     *
     * @param data
     */
    public void enqueue(String data) {
        Node node = new Node(data, null);
        if (tail == null) {
            // 空
            head = node;
            tail = node;
        } else {
            tail.next = node;
            tail = tail.next;
        }
    }

    /**
     * 出队
     *
     * @return
     */
    public String dequeue() {
        if (head == null) {
            return null;
        }
        String data = head.data;
        head = head.next;
        if (head == null) {
            // 只有一个节点，出队一个，tail设为null
            tail = null;
        }
        return data;
    }

    public void print() {
        Node p = head;
        while (p != null) {
            System.out.println(p.data);
            p = p.next;
        }
    }

    /**
     * 链表
     */
    static class Node {
        private String data;
        private Node next;

        public Node(String data, Node next) {
            this.data = data;
            this.next = next;
        }

        public String getData() {
            return data;
        }

        public void setData(String data) {
            this.data = data;
        }

        public Node getNext() {
            return next;
        }

        public void setNext(Node next) {
            this.next = next;
        }
    }
}